<?php
class E_books extends CI_Controller{
	function __construct(){
		parent::__construct();
		//判断session
		if (!$this->session->userdata('name')){
			header("Location: /");
		}
	}
	
	function index(){
		$this->load->model('e_books_model');
		$status = $this->input->get('status');
		$op = $this->input->get('op');
		$key = trim($this->input->get('key'));
		$pid = $this->input->get('pid');
		$language = $this->input->get('language');
		$offset = $this->input->get('offset');
		
		$where = array(
			'op' => $op,
			'key' => $key,
			'pid' => $pid,
			'language' => $language,
			'status' => $status,
		);
		
		$this->load->library('pagination');
		$page_config = render_pagination_config_array();	
		$page_config['base_url'] = '/e_books/?op='.$op.'&key='.$key.'&pid='.$pid.'&language='.$language.'&status='.$status;
		$page_config['total_rows'] = $this->e_books_model->get_all_rows($where);
		$this->pagination->initialize($page_config);
		$pager_link	= $this->pagination->create_links();
		
		$e_books_list = $this->e_books_model->get_consumable_by_page($page_config['per_page'],$offset,$where);
		
		$data['sidebar'] = array('editorial','e_books');
		$data['query'] = $e_books_list;
		$data['page'] = $pager_link;
		$data['search'] = $where;
		$a = array(''=>'未指定出版社');
		foreach(publishers_options() as $row){
			$a[$row['id']] = $row['publishername'];
		}
		$data['op'] = $a;
		load_view('e_books',$data);
	}
	
	function new_e_books($id){
		$this->load->model('entity_books_model');
		$obj = new Entity_books_model($id);
		$data['sidebar'] = array('editorial','e_books/new_e_books');
		$data['query'] = array(
			'modify' => false,
			'id' => '',
			'bid' => $id,
			'title' => $obj->title,
			'pid' => $obj->pid,
			'isbn' => $obj->isbn,
			'language' => '中',
			'format' => '',
			'intro' => '',
			'bok_size' => '',
			'time' => '',
			'project_numbers' => '',
			'status' => '2',
		);
		load_view('e_books_file',$data);
	}
	
	function modify_e_books($id){	
		$this->load->model('e_books_model');
		$obj = new E_books_model($id);
		$data['sidebar'] = array('editorial','e_books/modify_e_books');
		$data['query'] = array(
			'modify' => true,
			'id' => $id,
			'bid' => $obj->bid,
			'pid' => $obj->pid,
			'title' => $obj->title,
			'language' => $obj->language,
			'format' => $obj->format,
			'intro' => $obj->intro,
			'bok_size' => $obj->bok_size,
			'time' => $obj->time,
			'project_numbers' => $obj->project_numbers,
			'status' => $obj->status,
			'isbn' => $obj->isbn,
		);
		load_view('e_books_file',$data);
	}
	
	function execute(){
		$id = $this->input->post('id');
		$bid = $this->input->post('bid');
		$title = trim($this->input->post('title'));
		$pid = $this->input->post('pid');
		$language = $this->input->post('language');
		$format = $this->input->post('format');
		$intro = $this->input->post('intro');
		$bok_size = trim($this->input->post('bok_size'));
		$time = trim($this->input->post('time'));
		$project_numbers = $this->input->post('project_numbers');
		$status = $this->input->post('status');
		$isbn = $this->input->post('isbn');
	
		$this->load->model('e_books_model');
		$obj = new E_books_model($id);
		$obj->bid = $bid;
		$obj->title = $this->db->escape($title);
		$obj->pid = $pid;
		$obj->language = $language;
		$obj->format = $format;
		$obj->intro = $this->db->escape($intro);
		$obj->bok_size = $bok_size;
		$obj->time = $time;
		$obj->project_numbers = $project_numbers;
		$obj->status = $status;
		$obj->isbn = $isbn;
		
		if($id){
			$obj->update();
		}else{
			$obj->created = time();
			$obj->save();	
		}
		header("Location: /e_books");
	}
	
	function del_e_books($id){
		$this->load->model('e_books_model');
		$obj = new E_books_model($id);
		$obj->delete();
		header("Location: /e_books");
	}
}